package com.ewanghuiju.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ewanghuiju.app.R;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import static android.content.Context.WINDOW_SERVICE;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/17
 */
public class XieyiDialog extends Dialog implements View.OnClickListener {
    private TextView xieyi_text, tv_xieyi_no, tv_xieyi_yes;
    private ClockResult clockResult;
    private Context context;

    public XieyiDialog(@NonNull Context context, ClockResult clockResult) {
        super(context, R.style.MyBottomDialogStyle);
        this.clockResult = clockResult;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xieyi_dialog);
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        //获取到LayoutParams
        WindowManager.LayoutParams dialog_window_attributes = getWindow().getAttributes();
        //获取屏幕对象
        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        Display defaultDisplay = windowManager.getDefaultDisplay();
        //获取屏幕的宽、高，单位是像素
        int width = defaultDisplay.getWidth();
        int height = defaultDisplay.getHeight();
        //设置宽度
        dialog_window_attributes.width = 2 * width / 3;
        //设置高度
        getWindow().setAttributes(dialog_window_attributes);
        initView();
    }

    private void initView() {
        xieyi_text = findViewById(R.id.xieyi_text);
        tv_xieyi_no = findViewById(R.id.tv_xieyi_no);
        tv_xieyi_yes = findViewById(R.id.tv_xieyi_yes);
        tv_xieyi_no.setOnClickListener(this);
        tv_xieyi_yes.setOnClickListener(this);

        try {
            SpannableStringBuilder spannable = new SpannableStringBuilder("请你务必审慎阅读，充分理解\"服务协议“\"和“隐私政策\"各条款,包括但不限于:为了向你地理位置、内容分享等服务，我们需要收集你的设备信息、操作日志等个人信息。你可以在“设置“中查看，变更、删除个人信息并管理你的授权。你可阅读《服务协议》和《隐私政策》了解详细信息。如你同意，请点击同意开始接受我们的服务。");
            //设置文字的前景色
            spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary)), 111, 117, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.colorPrimary)), 118, 124, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //这个一定要记得设置，不然点击不生效
            xieyi_text.setMovementMethod(LinkMovementMethod.getInstance());
            spannable.setSpan(new TextClick(), 111, 117, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new TextClick(), 118, 124, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            xieyi_text.setText(spannable);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_xieyi_no:
                this.dismiss();
                if (clockResult != null)
                    clockResult.onNo();
                break;
            case R.id.tv_xieyi_yes:
                this.dismiss();
                if (clockResult != null)
                    clockResult.onYes();
                break;
            default:
                break;
        }
    }

    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
            clockResult.onLookxieyi();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
        }
    }

    public interface ClockResult {
        void onNo();

        void onYes();

        void onLookxieyi();
    }
}
